﻿Imports System.Data.SqlClient
Public Class FrmLDat
    Public m_Kyhieu As Boolean = False
    Public m_TenLoaiDat As Boolean = False
    Dim from As Object

    Private Sub loadData(ByVal strsql As String)
        Dim DA As New SqlDataAdapter(strsql, ob_cnn)
        Dim DS As New DataSet
        DA.Fill(DS)
        DataGridLoaiDat.DataSource = DS.Tables(0)
    End Sub
    Public Sub NapDanhSachVaoListBox()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select kyhieu, tenld from dbo.loaidat", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridLoaiDat.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub FrmLoaiDat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NapDanhSachVaoListBox()
    End Sub
    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

    Private Sub DataGridLoaiDat_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridLoaiDat.CellClick
        xuat_loai_dat()
    End Sub
    Sub xuat_loai_dat()
        Dim dong_hien_hanh As Integer = DataGridLoaiDat.CurrentRow.Index
        TextKyHieu.Text = DataGridLoaiDat.Rows(dong_hien_hanh).Cells(0).Value
        TextTenLoaiDat.Text = DataGridLoaiDat.Rows(dong_hien_hanh).Cells(1).Value
    End Sub

    Private Sub ButtonThêm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThêm.Click
        Try
            '  If MsgBox("Bạn có muốn lưu không ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Luu thông ting ") = MsgBoxResult.Yes Then

            If TextKyHieu.Text = "" Or TextTenLoaiDat.Text = "" Then
                MsgBox(" Nhập thông tin không đầy đủ !!! ")
            Else
                Dim lenh As String = ""
                lenh = "insert into LOAIDAT (kyhieu, tenLD )values('" & TextKyHieu.Text & "', N'" & TextTenLoaiDat.Text & "')"
                Dim cmd As New SqlCommand(lenh, ob_cnn)
                ob_cnn.Open()
                cmd.ExecuteNonQuery()
                ob_cnn.Close()
                loadData("select kyhieu, tenLD from Loaidat  ") 'lam tuoi du lieu

                MsgBox("Thêm thông tin thành công!")
                ob_cnn.Close()
            End If
            '  End If
        Catch ex As Exception

            If ex.Message.Contains("duplicate values in the index, primary key, or relationship") Then
                ' MessageBox.Show("Trùng mã, nhập lại!")
                ob_cnn.Close()
            Else
                MessageBox.Show("Ký hiệu loại đất đã trùng, nhập lại !!!")
                ob_cnn.Close()
            End If
        End Try
    End Sub
    Public Function checkID(ByVal Table As String, ByVal ID As String, ByVal IDvalue As String) As Integer
        ob_cnn.Open()
        Dim sqlCom As New SqlCommand("select Kyhieu from loaidat where kyhieu= '" & TextKyHieu.Text & "'", ob_cnn)
        Dim count As Int32 = Convert.ToInt32(sqlCom.ExecuteScalar())
        ob_cnn.Close()
        Return count
    End Function

    Private Sub ButtonSua_Click(sender As Object, e As EventArgs) Handles ButtonSua.Click
        Try
            If TextKyHieu.Text = "" Or TextTenLoaiDat.Text = "" Then
                MsgBox(" Nhập thông tin không đầy đủ !!! ")
            Else
                Dim lenh As String = ""
                lenh = "update LOAIDAT set tenLD=N'" & TextTenLoaiDat.Text & "' where  KyHieu = '" & TextKyHieu.Text & "'"
                Dim cmd As New SqlCommand(lenh, ob_cnn)
                ob_cnn.Open()
                cmd.ExecuteNonQuery()
                ob_cnn.Close()
                loadData("select kyhieu, tenLD from Loaidat ") 'lam tuoi du lieu
                MsgBox("Sửa thông tin thành công!")
                ob_cnn.Close()
            End If

        Catch ex As Exception
            If ex.Message.Contains("duplicate values in the index, primary key, or relationship") Then
                ob_cnn.Close()
            Else
                MessageBox.Show("Thông tin đã trùng, nhập lại !!!")
                ob_cnn.Close()
            End If
        End Try
    End Sub

    Private Sub DataGridLoaiDat_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridLoaiDat.CellContentClick

    End Sub
End Class